import React, { useState, useEffect, useCallback } from 'react';
import { getSofBillingOverview, postSofBillingAccountQuery } from '../../api';
import SelectModule from './Components/SelectModule';
import AddModule from './Components/AddModule'
import { CollectionStatisticsListAll } from './style';
import { PageHeader, Col, Statistic, Row, Table, message } from 'antd';
import moment from 'moment';

/**
 * @description: 财务管理 -- 回款统计
 * @author: xiaoxujs
 * @updata: xiaoxujs（2021-11-20）创建页面
 * @updata: xiaoxujs（2021-11-22）获取接口
 */

const CollectionStatisticsList = () => {
    const [listData, setListData] = useState([])
    const [overViewData, setOverViewData] = useState({})
    const [total, setTotal] = useState(0)
    const [currentPage, setCurrentPage] = useState(1)
    const [size, setSize] = useState(10)
    const [loading, setLoading] = useState(false)
    const [selectData, setSelectData] = useState({})
    const [addModuleShow, setAddModuleShow] = useState(false);
    const overViewFun = useCallback(
        () => {
            ; (async () => {
                const { code, msg, data } = await getSofBillingOverview()
                if (code === '20000') {
                    setOverViewData(data)
                } else {
                    message.error(msg)
                }
            })()
        },
        [],
    )
    const listFun = useCallback(
        (parames = {
            currentPage: 1,
            size: 10
        }) => {
            ; (async () => {
                const { code, msg, data } = await postSofBillingAccountQuery(parames)
                if (code === '20000') {
                    setListData(data.records)
                    setTotal(data.total)
                    setCurrentPage(parames.currentPage)
                    setSize(parames.size)
                    setLoading(false)
                } else {
                    message.error(msg)
                }
            })()
        },
        [],
    )
    useEffect(() => {
        overViewFun()
        listFun()
    }, [overViewFun, listFun])
    // 搜索
    const handleSelect = (values) => {

        if(values.time) {
            values.startDate = moment(values.time[0]).format('YYYY-MM-DD');
            values.endDate = moment(values.time[1]).format('YYYY-MM-DD');
            delete values.time
        }
        setSelectData(values)
        const parames = {
            currentPage: 1,
            size: 10,
            ...values
        }
        listFun(parames);
    }
    //分页
    const pagination = {
        //是否可以改变pagesize//
        showSizeChanger: true,
        //是否可以快速跳转到某页
        showQuickJumper: true,
        //展示一共有多少条数据//
        showTotal: () => `共${total}条`,
        //每页条数
        pageSize: size,
        //当前页数
        current: currentPage,
        //数据总数
        total: total,
        //pageSize 变化的回调
        onShowSizeChange: (current, pageSize) => changePageSize(pageSize, current),
        //页码改变的回调，参数是改变后的页码及每页条数
        onChange: (current) => changePage(current),
    }
    //改变每页条数
    const changePageSize = (pageSizeFun, current) => {
        const page = {
            currentPage: current,
            size: pageSizeFun
        }
        const parames = Object.assign(selectData, page)
        listFun(parames);
    }
    //改变页数
    const changePage = (current) => {
        const page = {
            currentPage: current,
            size
        }
        const parames = Object.assign(selectData, page)
        listFun(parames);
    }
    const columns = [
        {
            title: '客户名称',
            dataIndex: 'clientName'
        },
        {
            title: '合同编号',
            dataIndex: 'conNo'
        },
        {
            title: '业务员',
            dataIndex: 'sellName'
        },
        {
            title: '实收金额',
            dataIndex: 'actAccounts'
        },
        // {
        //     title: '操作',
        //     render: (text, recode) => <Space>
        //         <Button type='primary' onClick={() => handleAddBillingRegistration(recode.id)} >开票登记</Button>
        //     </Space>
        // }
    ];
    return <CollectionStatisticsListAll>
        <PageHeader
            className="site-page-header"
            title="回款统计"
        />
        <SelectModule listFun={listFun} handleSelect={handleSelect} />
        <Row gutter={16} className='data-show'>
            <Col span={8}>
                <Statistic title="合同" value={overViewData.contractTotal} />
            </Col>
            <Col span={8}>
                <Statistic title="实收" value={overViewData.paid} />
            </Col>
            <Col span={8}>
                <Statistic title="收缴率" value={overViewData.confiscate} />
            </Col>
        </Row>
        <Table dataSource={listData} columns={columns} pagination={pagination} rowKey='id' loading={loading} />
        <AddModule 
            setAddModuleShow = {setAddModuleShow}
            addModuleShow = {addModuleShow}
        />
    </CollectionStatisticsListAll>
}

export default CollectionStatisticsList;